package com.qls.web.modules.dt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qls.core.entity.DtStore;
import com.qls.web.common.utils.PageUtils;
import com.qls.web.common.utils.Query;
import com.qls.web.modules.dt.dao.StoreDao;
import com.qls.web.modules.dt.service.StoreService;
import com.qls.web.modules.vo.StoreVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * @author doyoung
 */
@Service("storeService")
public class StoreServiceImpl extends ServiceImpl<StoreDao, DtStore> implements StoreService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DtStore> page = this.page(
                new Query<DtStore>().getPage(params),
                new QueryWrapper<DtStore>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils sqlQueryPage(Map<String, Object> params) {
        IPage<StoreVO> page = new Query<StoreVO>().getPage(params);
        List<StoreVO> list = null;
        params.put("page", page);
        list = baseMapper.sqlQueryPage(params);
        return new PageUtils(page, list);
    }

    @Override
    public List<DtStore> queryByDtId(Map<String, Object> params) {
        Object dtId = params.get("dtId");
        String inSql = "SELECT id FROM dt_dt WHERE FIND_IN_SET(id, getChild(" + dtId + "))";
        List<DtStore> list = baseMapper.selectList(new QueryWrapper<DtStore>()
                .inSql(dtId != null, "dt_id", inSql));
        return list;
    }

    @Override
    public DtStore getByGroupId(Integer groupId) {
        return baseMapper.getByGroupId(groupId);
    }
}